//
//  LBView.m
//  Leisure
//
//  Created by 沈浩hh on 16/3/18.
//  Copyright © 2016年 HHUI. All rights reserved.
//

#import "LBView.h"
#import "ScrollViewViewController.h"

@interface LBView ()<UIScrollViewDelegate>
@property(nonatomic, strong)UIScrollView *scrollView;
@property(nonatomic, strong)UIPageControl *page;
@property(nonatomic, strong)UIImageView *scrollImageView;
@property(nonatomic, strong)NSTimer *timer;
@property(nonatomic, assign)NSInteger count;
@property(nonatomic, strong)ScrollViewViewController *scrollVc;
@end

@implementation LBView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.scrollView = [[UIScrollView alloc]init];
    }
    return self;
}

- (UIView *)addScrollView:(CGRect)scrollViewFrame iconArray:(NSMutableArray *)array
{
    _count = array.count;
    _scrollView.frame = scrollViewFrame;
    _scrollView.contentSize = CGSizeMake(KWIDTH * (array.count + 2), 0);
    _scrollView.contentOffset = CGPointMake(KWIDTH, 0);
    _scrollView.pagingEnabled = YES;
    _scrollView.bounces= NO;
    
    _scrollView.showsHorizontalScrollIndicator = NO;
    
    _scrollView.showsVerticalScrollIndicator = NO;
    _scrollView.delegate = self;
    
    UIImageView *tempImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, KWIDTH, _scrollView.frame.size.height)];
    [tempImageView sd_setImageWithURL:[NSURL URLWithString:array[array.count - 1]]];
    [_scrollView addSubview:tempImageView];
    UIImageView *tempImageView1 = [[UIImageView alloc]initWithFrame:CGRectMake(KWIDTH * (array.count + 1), 0, KWIDTH, _scrollView.frame.size.height)];
    [tempImageView1 sd_setImageWithURL:[NSURL URLWithString:array[0]]];
    [_scrollView addSubview:tempImageView1];
    for (int i = 0; i < _count ; i++) {
        _scrollImageView = [[UIImageView alloc]initWithFrame:CGRectMake(KWIDTH * (i + 1), 0, KWIDTH, _scrollView.frame.size.height)];
        
        [_scrollImageView sd_setImageWithURL:[NSURL URLWithString:array[i]]];
        [_scrollView addSubview:_scrollImageView];
    }
    
    // 添加手势
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
    [_scrollView addGestureRecognizer:tap];
    // 滚动视图下面的圆点
    _page = [[UIPageControl alloc]initWithFrame:CGRectMake(KWIDTH - 50 - 20,   scrollViewFrame.size.height - 15 - 10, 50, 20)];
    //    page.backgroundColor = [UIColor grayColor];
    _page.numberOfPages = array.count;
    _page.currentPage = 0;
    [_page addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventValueChanged];
    [self addSubview:_scrollView];
    [self addSubview:_page];
    [self setUpTimer];
    return self;
}
#pragma mark ---- scrollView -----
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger index = scrollView.contentOffset.x / KWIDTH;
    _page.currentPage = index - 1;
    if (index == _count + 1) {
        [scrollView setContentOffset:CGPointMake(KWIDTH * 1, 0) animated:NO];
        _page.currentPage = 0;
    } else if (index == 0) {
        [scrollView setContentOffset:CGPointMake(KWIDTH * _count, 0) animated:NO];
        _page.currentPage = _count;
    }
}

#pragma mark ---- UIPageControl的触发方法 ----

- (void)pageAction:(UIPageControl *)page
{
    [self.scrollView setContentOffset:CGPointMake((page.currentPage + 1) * KWIDTH, 0) animated:YES];
}

- (void)timerAction
{
    NSInteger index = _scrollView.contentOffset.x / KWIDTH;
    _page.currentPage = index;
    [_scrollView setContentOffset:CGPointMake((index + 1) * KWIDTH, 0) animated:YES];
    if (index == _count + 1) {
        [_scrollView setContentOffset:CGPointMake(KWIDTH * 1, 0) animated:NO];
        _page.currentPage = 0;
        [_scrollView setContentOffset:CGPointMake(KWIDTH * 2, 0) animated:YES];
        _page.currentPage = 1;
    } else if (index == 0) {
        [_scrollView setContentOffset:CGPointMake(KWIDTH * _count, 0) animated:NO];
        _page.currentPage = _count;
    }
    if (index == _count) {
        _page.currentPage = 0;
    }
}

- (void)setUpTimer
{
    _timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [_timer invalidate];
    _timer = nil;
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    [self setUpTimer];
}


- (void)tapAction:(UITapGestureRecognizer *)tap
{

    ScrollViewViewController *scrollVc = [[ScrollViewViewController alloc]init];
    self.nav.tabBarController.tabBar.hidden = YES;
    scrollVc.article_id = _array[_page.currentPage];
     [self.nav pushViewController:scrollVc animated:YES];
}


@end
